package bolangit.test.youbike.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wjf
 * @description TODO 全局捕获异常和自定义全局捕获异常
 * @date 2019/1/26 23:40
 */
@ControllerAdvice
public class MyControllerAdvice {

    /**
     * 全局异常处理，反正异常返回统一格式的map
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map<String, Object> exceptionHandler(Exception ex) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 1001);
        map.put("mag", ex.getMessage());
        return map;
    }
}
